<?php

namespace App\Http\Controllers\Api;

use App\Models\Site;
use JonnyW\PhantomJs\Client;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;

class ShotController extends Controller
{
    //
    public function search($id)
    {
        $site    = Site::where('id', $id)->first();
        $url    = $this->check($site->url);
        if($url){
            $client = Client::getInstance();
            $client->getEngine()->setPath(base_path().'/bin/phantomjs');
            $client->getEngine()->addOption('--load-images=true');
            $client->getEngine()->addOption('--ignore-ssl-errors=true');
            $client->isLazy();
            $top    = 0;
            $left   = 0;
            $width  = 1280;
            $height = 800;

            /**
             * @see JonnyW\PhantomJs\Http\CaptureRequest
             **/
            $res = $client->getMessageFactory()->createCaptureRequest($url, 'GET');
            $res->setOutputFile(storage_path('app/public/shot/'.$site->url.'.jpg'));
            $res->setViewportSize($width, $height);
            $res->setCaptureDimensions($width, $height, $top, $left);
            $res->setTimeout(5000);//超过指定时间则中断渲染
            $res->setDelay(5);
            /**
             * @see JonnyW\PhantomJs\Http\Response
             **/
            $response = $client->getMessageFactory()->createResponse();
            $client->send($res, $response);
            $this->ziyuan($id);
            return response()->json(['msg' => '数据已经获取成功!3秒后,刷新页面', 'code' => '0'],200);
        }
        return response()->json(['msg' => '您需要的数据!出现问题,正在修理过程中……请稍候重试!……', 'code' => '1'],200);
    }
    public function check($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://'.$url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'X-FORWARDED-FOR:220.181.108.91',
            'CLIENT-IP:220.181.108.91'
        ]);//伪造百度蜘蛛IP
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//若给定url自动跳转到新的url,有了下面参数可自动获取新url内容：302跳转
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//设置cURL允许执行的最长秒数。
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"); //伪造百度蜘蛛头部
        curl_setopt($ch, CURLOPT_REFERER, $url);
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate'); //这个是解释gzip内容
        $data = curl_exec($ch);
        $code = curl_getinfo($ch);
        curl_close($ch);
        if($code['http_code'] == 200){
            $parse_url  = parse_url($code['url']);
            return      $parse_url['scheme'].'://'.$parse_url['host'];
        }else{
            return false;
        }
    }
    function ziyuan($id)
    {
        $urls = array(
            env('APP_URL').'/siteinfo-'.$id.'.html',
        );
        $api = env('BAIDUZHANZHANG');
        $ch = curl_init();
        $options =  array(
            CURLOPT_URL => $api,
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS => implode("\n", $urls),
            CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
        );
        curl_setopt_array($ch, $options);
        $result = curl_exec($ch);
        return $result;
    }
}
